package com.yj.web.service.impl;

import com.mysql.cj.core.util.StringUtils;
import com.yj.cache.service.YjCacheService;
import com.yj.web.BussCode;
import com.yj.web.dao.mapper.EmployeeMapper;
import com.yj.web.dao.po.admin.Area;
import com.yj.web.dao.po.admin.Department;
import com.yj.web.dao.po.admin.Duty;
import com.yj.web.dao.po.admin.Employee;
import com.yj.web.help.PoUtil;
import com.yj.web.service.EmployeeService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yesiming on 15/5/5.
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    private static final String LOGIN_KEY = "yj.yfq.employee.code.";
    private static final int EXP_TIME = 60 * 5;

    @Autowired
    private YjCacheService passportCache;

    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee getEmployee(long employeeId) {
    	return employeeMapper.findEmployeeById(employeeId);
    }

    @Override
    public long createEmployee(Employee employee) {
        String password = employee.getPassword();
        if (null == password || "" ==password) {
            password = "123456";
        }
        employee.setPassword(DigestUtils.md5Hex(password));
        return employeeMapper.insertEmployee(employee);

    }

    @Override
    public Employee findEmployeeByPhone(String phone) {
        return employeeMapper.findEmployeeByPhone(phone);
    }

    @Override
    public String updateEmployee(Employee employee) {
        Employee tmpEmployee = employeeMapper.findEmployeeById(employee.getId());
        if(tmpEmployee == null) {
            return BussCode.FAIL;
        }
        try {
            PoUtil.supplementPo(employee, tmpEmployee);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!employeeMapper.updateEmployeeInfoById(tmpEmployee)) {
            return BussCode.FAIL;
         }
        return BussCode.SUCCESS;
    }


    @Override
    public Employee loginByMsg(String phone, String code) {
        // 1. 去数据库查找手机号用户是否存在
        Employee employee = employeeMapper.findEmployeeByPhone(phone);
        if(employee == null) {
            return null;
        }

        // 1. 从redis里取得手机号对应的验证码
        String coded = passportCache.get("yj.ms.pt.wb.code." + phone);
        if(StringUtils.isNullOrEmpty(coded)) {
            return null;
        }

        // 2. 比较验证码是否一致
        if(!coded.equals(code)) {
            return null;
        }

        return employee;
    }

    @Override
    public Employee loginByPhonePasswd(String phone, String passwd) {
        passwd = DigestUtils.md5Hex(passwd);
        Employee employee = employeeMapper.findEmployeeByPhoneAndPassword(phone, passwd);

        return employee;
    }

    @Override
    public Employee findEmployeeById(long id) {
        Employee employee = employeeMapper.findEmployeeById(id);
        employee.setManagerName(employeeMapper.getNameById(id));
        return employee;
    }

    @Override
    public boolean checkMsgCode(String phone, String code) {
        // 1. 从redis里取得手机号对应的验证码
        String coded = passportCache.get(LOGIN_KEY + phone);
        if(StringUtils.isNullOrEmpty(coded)) {
            return false;
        }

        // 2. 比较验证码是否一致
        if(!coded.equals(code)) {
            return false;
        }

        return true;
    }

    @Override
    public void setLoginKeyByMsg(String phone, String code) {
        passportCache.set(LOGIN_KEY + phone, code, EXP_TIME);
    }

    @Override
    public List<Employee> listEmployee(String name, String department, String phone, int page, int size) {
        int offset = (page - 1) * size;
        List<Employee> employees = employeeMapper.listEmployee(name, department, phone, offset, size);
        
        return employees;
    }

    @Override
    public int countEmployee(String name, String department, String phone) {
        int count = employeeMapper.countEmployee(name, department, phone);
        return count;
    }

    @Override
    public boolean deleteEmployeeById(int id) {
        Employee employee = employeeMapper.findEmployeeById(id);
        if (null == employee) {
            return false;
        } else {
            employeeMapper.deleteById(id);
            return true;
        }
    }

    @Override
    public boolean resetPassword(Employee employee) {
        return employeeMapper.resetPassword(employee);
    }

    @Override
    public int insertDuty(String name) {
        return employeeMapper.insertDuty(name);
    }

    @Override
    public List<Duty> listDuty() {
        return employeeMapper.listDuty();
    }

    @Override
    public int insertDepartment(String name) {
        return employeeMapper.insertDepartment(name);
    }
    @Override
    public List<Department> listDepartment() {
        return employeeMapper.listDepartment();
    }

    @Override
    public int insertArea(String name) {
        return employeeMapper.insertArea(name);
    }
    @Override
    public List<Area> listArea() {
        return employeeMapper.listArea();
    }

    @Override
    public List<Employee> listManager() {
        return employeeMapper.listManager();
    }

    @Override
    public boolean updateEmployeeStatus(long id, int isActive) {
        return employeeMapper.updateEmployeeStatus(id, isActive);
    }
}
